package com.kitty.web.system.send.service;


import com.kitty.common.model.Reason;
import com.kitty.common.utils.SpringUtils;
import com.kitty.cxf.common.MapStringUtil;
import com.kitty.game.chat.service.ChatService;
import com.kitty.game.enter.TitleInfo;
import com.kitty.game.equip.message.RespNotifyMiscEx;
import com.kitty.game.equip.service.EquipService;
import com.kitty.game.equip.service.FabaoService;
import com.kitty.game.fight.service.CountService;
import com.kitty.game.gm.handler100.NCChongzhi;
import com.kitty.game.gm.handler100.NCHaoXinZhi;
import com.kitty.game.gm.handler100.NCMiJingJiFei;
import com.kitty.game.notice.model.Notice;
import com.kitty.game.pay.model.PayNotifyInfo;
import com.kitty.game.pay.service.OrderService;
import com.kitty.game.pet.PetDataPool;
import com.kitty.game.pet.PetType;
import com.kitty.game.pet.bean.PetObject;
import com.kitty.game.pet.model.Pet;
import com.kitty.game.pet.service.PetService;
import com.kitty.game.role.model.Account;
import com.kitty.game.role.model.AccountLogin;
import com.kitty.game.role.model.Invite;
import com.kitty.game.role.model.Role;
import com.kitty.game.role.service.AccountService;
import com.kitty.game.role.service.PayService;
import com.kitty.game.team.message.RespMsg;
import com.kitty.game.utils.PrivilegeConst;
import com.kitty.mina.message.MessagePusher;
import com.kitty.web.common.AjaxResult;
import com.kitty.web.system.send.domain.*;
import lombok.extern.slf4j.Slf4j;
import org.nutz.dao.Cnd;
import org.nutz.dao.Dao;
import org.nutz.lang.random.R;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @author soul
 * 1：元宝、积分、抽奖次数，2：发送法宝，3：发送宠物，4：发送随机属性首饰，5：在线发送坐骑，6：发送称谓
 */
@Service
@Slf4j
public class SendServiceImpl implements SendService
{


    /**
     * 发送【法宝】
     */
    @Override
    public AjaxResult magicWeapon(SendMagicWeapon magic) {
        try {
            Role role = SpringUtils.getRoleService().getOnlinePlayer(magic.getName());
            if (role == null) {
                return AjaxResult.error("角色不在线");
            }
            SpringUtils.getBean(FabaoService.class).getArtifact(role, magic.getMagicWeapon(), Integer.valueOf(magic.getPolarity()), magic.getLevel());
            MessagePusher.pushMessage(role, new RespNotifyMiscEx("获得" + magic.getLevel() + "阶法宝：" + magic.getMagicWeapon()));
            return AjaxResult.success("发送成功");
        } catch (Exception e) {
            return AjaxResult.error();
        }
    }

    /**
     * 发送【称谓】
     */
    @Override
    public AjaxResult title(SendTitle title) {
        try {

            Role role = SpringUtils.getRoleService().getOnlinePlayer(title.getName());
            if (role == null) {
                return AjaxResult.error("角色不在线");
            }
            TitleInfo titleInfo = new TitleInfo();
            titleInfo.setType(title.getTitle());
            titleInfo.setTitle(title.getTitle());
            SpringUtils.getRoleService().addTitle(role, titleInfo);
            return AjaxResult.success("发送成功");
        } catch (Exception e) {
            log.info("操作异常",e);
            return AjaxResult.error("操作异常");
        }
    }

    /**
     * 发送【元宝、积分、抽奖次数】
     */
    @Override
    public AjaxResult coin(SendCoin coin) {
        try {
            Role role = SpringUtils.getRoleService().getOnlinePlayer(coin.getName());
            int money = coin.getNum();
            int type = coin.getType();
            if (role == null) {
                return AjaxResult.error("角色不在线");
            }
            if (type == 0) {
                SpringUtils.getRoleService().addGold(role, money, Reason.GM_ADD_GOLD);
                SpringUtils.getRoleService().updateRoleGoldAndSiver(role);
                MessagePusher.pushMessage(role, new RespNotifyMiscEx("获得金元宝：" + money));
            } else if (type == 1) {
                SpringUtils.getRoleService().addRechargeScore(role, money);
                MessagePusher.pushMessage(role, new RespMsg("获得积分" + money));
            } else if (type == 2) {
                SpringUtils.getBean(PayService.class).choujiang(role, money);
                MessagePusher.pushMessage(role, new RespMsg("获得抽奖次数" + money));

            } else if (type == 3) {
                NCChongzhi ncChongzhi = new NCChongzhi();
                ncChongzhi.handle(role,money+"");
                MessagePusher.pushMessage(role, new RespMsg("获得充值" + money));
            } else if (type == 4) {
                NCMiJingJiFei ncMiJingJiFei = new NCMiJingJiFei();
                ncMiJingJiFei.handle(role,money+"");
                MessagePusher.pushMessage(role, new RespMsg("获得奇门秘境积分" + money));
            } else if (type == 5) {
                NCHaoXinZhi nCHaoXinZhi = new NCHaoXinZhi();
                nCHaoXinZhi.handle(role,money+"");
                MessagePusher.pushMessage(role, new RespMsg("获得好心值" + money));
            }
            return AjaxResult.success("发送成功");
        } catch (Exception e) {
            return AjaxResult.error("操作异常");
        }
    }

    /**
     * 发送【宠物】
     */
    @Override
    public AjaxResult pet(SendPet pet) {
        try {

            Role role = SpringUtils.getRoleService().getOnlinePlayer(pet.getName());
            if (role == null) {
                return AjaxResult.error("角色不在线");
            }
            PetObject petObject = PetDataPool.getPetObject(pet.getGoodsName());
            if (petObject == null) {
                return AjaxResult.error("宠物不存在");
            }

            petObject.setSpeed((short)pet.getSpeed().intValue());
            petObject.setPhy_attack((short)pet.getPhyAttack().intValue());
            petObject.setMana((short)pet.getMana().intValue());
            petObject.setMag_attack((short)pet.getMagAttack().intValue());
            petObject.setLife((short)pet.getLife().intValue());
            if(pet.getDegree() !=null && pet.getDegree()>0){
                petObject.setCapacity_level((byte)pet.getDegree().intValue());
            }
            if (petObject.getPetType() == PetType.ghost ? !SpringUtils.getPetService().isEquippedGhostFull(role) : !SpringUtils.getPetService().isEquippedFull(role)) {
                Pet pet_ = SpringUtils.getBean(PetService.class).addPet(petObject, role, false);
                SpringUtils.getBean(PetService.class).loadPet(role, pet_);
                MessagePusher.pushMessage(role, new RespNotifyMiscEx("获得宠物：" + pet.getGoodsName()));
                return AjaxResult.success("发送成功");
            } else {
                return AjaxResult.error("玩家宠物包裹已满");
            }
        } catch (Exception e) {
            return AjaxResult.error("操作异常");
        }
    }


    @Override
    public AjaxResult sendItem(SendItem sendItem) {
        try {
            Role role = SpringUtils.getRoleService().getOnlinePlayer(sendItem.getName());
            if (role == null) {
                return AjaxResult.error("角色不在线");
            }
            SpringUtils.getBean(EquipService.class).addMall(sendItem.getItemName(), true, role, Integer.parseInt(sendItem.getItemNum()));
            return AjaxResult.success("发送成功");
        } catch (Exception e) {
            return AjaxResult.error("操作异常");
        }
    }

    @Override
    public AjaxResult sendmoney(SendMoney sendMoney) {

        AccountLogin accountLogin = SpringUtils.getBean(AccountService.class).queryByUsername(sendMoney.getAccount());
        if(accountLogin !=null){
            PayNotifyInfo payNotifyInfo = new PayNotifyInfo();
            payNotifyInfo.setMoney(sendMoney.getMoney());
            payNotifyInfo.setTradeNo("MY-"+ R.UU32());
            payNotifyInfo.setAccount(accountLogin.getId());
            payNotifyInfo.setRoleUid(1L);
            //  payNotifyInfo.setGold(Integer.parseInt(strings[1]));
            payNotifyInfo.setCreateTime(new Date());
            SpringUtils.getBean(OrderService.class).handleOrder(payNotifyInfo);
            return AjaxResult.success("发送成功");
        }else{
            return AjaxResult.error("未查询到账号信息");
        }

    }

    @Override
    public AjaxResult sendNotice(SendNotice sendNotice) {
        SpringUtils.getBean(ChatService.class).sendAdnotice(sendNotice.getContent());
        Dao dao = SpringUtils.getBean(Dao.class);
        Notice notice = dao.fetch(Notice.class, Cnd.NEW());
        if (notice == null){
            notice = new Notice();
        }
        notice.setContent(sendNotice.getContent());
        dao.insertOrUpdate(notice);
        return AjaxResult.success("发送成功");
    }


    /**
     * 发送【首饰】
     */
    @Override
    public AjaxResult ornaments(SendOrnaments ornaments) {
        try {
//            Map client = sendMapper.Client(MapObjectUtil.objectToMap(ornaments),"ornaments");
//            if (client != null) {
//                if ("0".equals(client.get("code"))) {
//                    return AjaxResult.success();
//                } else {
//                    return AjaxResult.error(client.get("msg").toString());
//                }
//            }
            return AjaxResult.error();
        } catch (Exception e) {
            return AjaxResult.error();
        }
    }




    /**
     * 发送【装备】
     */
    @Override
    public AjaxResult equipment(SendEquipment equipment) {

        Map map  = new HashMap<String,Object>();
        Map blue = new HashMap<String,Object>();
        Map pink =  new HashMap<String,Object>();
        Map yellow = new HashMap<String,Object>();
        Map green  = new HashMap<String,Object>();
        Map resonance  = new HashMap<String,Object>();
        Map remouldPro  = new HashMap<String,Object>();

        // 蓝色属性
        if (equipment.getBlueOne() != null ){
            blue.put(equipment.getBlueOne(),equipment.getBlueOneValue());
        }
        if (equipment.getBlueTwo() != null ){
            blue.put(equipment.getBlueTwo(),equipment.getBlueTwoValue());
        }
        if (equipment.getBlueThree() != null ){
            blue.put(equipment.getBlueThree(),equipment.getBlueThreeValue());
        }
        map.put("blue", MapStringUtil.character(blue));

        // 粉色属性
        if (equipment.getPink() != null ){
            pink.put(equipment.getPink(),equipment.getPinkValue());
        }
        map.put("pink",MapStringUtil.character(pink));

        // 黄色属性
        if (equipment.getYellow() != null){
            yellow.put(equipment.getYellow(),equipment.getYellowValue());
        }
        map.put("yellow",MapStringUtil.character(yellow));

        // 绿色属性
        if (equipment.getGreen() != null){
            green.put(equipment.getGreen(),equipment.getGreenValue());
        }
        map.put("green",MapStringUtil.character(green));

        // 绿色暗属性
        if (equipment.getRemouldPro() != null){
            remouldPro.put(equipment.getRemouldPro(),equipment.getRemouldProValue());
        }
        map.put("green_dark",MapStringUtil.character(remouldPro));

        // 共鸣属性（改造12属性）
        if (equipment.getResonance() != null){
            resonance.put(equipment.getResonance(),equipment.getResonanceValue());
        }
        map.put("resonance",MapStringUtil.character(resonance));



        // 装备类型
        map.put("groupNo", 1024);
        map.put("type",equipment.getType());
        // 装备等级
        map.put("level",equipment.getLevel());
        // 改造等级
        map.put("remouldLevel",equipment.getRemouldLevel());
        // 用户名称
        map.put("name",equipment.getName());
        // 装备名称
        map.put("goodsName",equipment.getGoodsName());
        // 五行
        map.put("polarity",equipment.getPolarity());

        try {
//            Map client = sendMapper.Client(map,"equip");
//            if (client != null) {
//                if ("0".equals(client.get("code"))) {
//                    return AjaxResult.success();
//                } else {
//                    return AjaxResult.error(client.get("msg").toString());
//                }
//            }
            return AjaxResult.error();
        } catch (Exception e) {
            return AjaxResult.error();
        }
    }




    public String getPolar(String index){
        String polars[] = {"无","金","木","水","火","土"};
        return String.valueOf(polars[Integer.valueOf(index)]);
    }

}
